package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private SearchFeignClient searchFeignClient;
    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;


    /**
     * 更新排行榜
     */
    @XxlJob(value = "distroScheduleUpdateRankingHandler")
    public void distroScheduleUpdateRanking(){
        XxlJobLog xxlJobLog = new XxlJobLog();
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(new Date());
        try {
            log.info("准备开始更新排行榜：{}",startTime);
            //更新排行榜   这里的远程调用有问题
            Result<Boolean> flagResult = searchFeignClient.preRankingCache();
            Boolean aBoolean = flagResult.getData();
            Assert.notNull(aBoolean,"远程调用专辑微服务更新排行榜失败");

            long jobId = XxlJobHelper.getJobId();
            xxlJobLog.setJobId(jobId);
            if (aBoolean){
                // 将这次定时任务指定的业务操作保存到
                xxlJobLog.setStatus(1); //如果更新成功 保存1 更新失败保存0
            }else {
                xxlJobLog.setStatus(0);
                xxlJobLog.setError("远程查询出现了异常");
            }

        }catch(Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
        } finally{
            long endTime = System.currentTimeMillis();
            xxlJobLog.setTimes((int) (endTime - currentTime)); //执行完任务的耗时
            xxlJobLogMapper.insert(xxlJobLog);
            log.info("排行榜信息更新完毕耗时：{}",endTime - currentTime);
        }
    }

    @XxlJob(value = "distroScheduleUpdateVip")
    public void distroScheduleUpdateVip(){
        XxlJobLog xxlJobLog = new XxlJobLog();

        long currentTime = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(new Date());
        try {
            log.info("准备开始更新vip身份信息 ：{}",startTime);
            //
            userInfoFeignClient.updateExpireVip();
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            xxlJobLog.setStatus(1);

        }catch(Exception e){
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
        } finally{
            long endTime = System.currentTimeMillis();
            xxlJobLog.setTimes((int) (endTime - currentTime)); //执行完任务的耗时
            xxlJobLogMapper.insert(xxlJobLog);
            log.info("vip身份信息更新完毕耗时：{}",endTime - currentTime);
        }
    }

}